﻿using FarPoint.Web.Spread;
using FarPoint.Web.Spread.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ControlExplorer.samples.OrderEntry
{
  public partial class Admin : System.Web.UI.Page
  {
    SqlConnection SqlConnection1 = new SqlConnection();
    SqlCommand SqlSelectCommand1 = new SqlCommand();
    SqlDataAdapter SqlDataAdapter1 = new SqlDataAdapter();
    DataSet DataSetPubs1 = new DataSet();
    //rows, columns
    DefaultSheetDataModel model = new DefaultSheetDataModel(2, 2);
    StyleInfo style = new StyleInfo();

    private void Page_Load(System.Object sender, System.EventArgs e)
    {
      //Put user code to initialize the page here
        this.FpSpread1.UseClipboard = false;
        InitSpread();
      ListOptions();

      if (this.InIE9PlusCompatView())
      {
        foreach (FarPoint.Web.Spread.FpSpread spread in this.GetSpreads(this))
        {
          FarPoint.Web.Spread.SheetView activeSheet = spread.ActiveSheetView;
          FarPoint.Web.Spread.Border border = activeSheet.DefaultStyle.Border;
          if (border == null || border.IsEmpty)
          {
            activeSheet.DefaultStyle.Border = new FarPoint.Web.Spread.Border(BorderStyle.Solid, activeSheet.GridLineColor, 1);
          }
        }
      }
    }

    private bool InIE9PlusCompatView()
    {
      if (this.Page.Request.Browser.MajorVersion == 7)
      {
        string userAgent = this.Page.Request.UserAgent;
        int index = userAgent.IndexOf("trident/", StringComparison.OrdinalIgnoreCase);
        int index2 = 0;
        if (index >= 0)
        {
          index2 = userAgent.IndexOf(";", index);
        }
        else
        {
          index2 = -1;
        }
        if (index2 > index && index >= 0)
        {
          double tridentVersion = 0;
          if (double.TryParse(userAgent.Substring(index + 8, index2 - (index + 8)), out tridentVersion))
          {
            return tridentVersion >= 5;
          }
        }
      }
      return false;
    }

    private System.Collections.IEnumerable GetSpreads(Control ctrl)
    {
      System.Collections.Generic.List<FarPoint.Web.Spread.FpSpread> list = new System.Collections.Generic.List<FarPoint.Web.Spread.FpSpread>();
      foreach (Control c in ctrl.Controls)
      {
        foreach (FarPoint.Web.Spread.FpSpread spread in GetSpreads(c))
        {
          list.Add(spread);
        }

        if (c is FarPoint.Web.Spread.FpSpread)
        {
          list.Add((FarPoint.Web.Spread.FpSpread)c);
        }
      }

      return list;
    }

    public void ListOptions()
    {
      MakeLinkCell(0, "销售部报表", "report.aspx");
      MakeLinkCell(1, "维护数据库", "maintaindb.aspx");

      model.SetValue(0, 1, "查看详细销售报表。");
      model.SetValue(1, 1, "查看或更新数据列表。");
    }

    public void MakeLinkCell(int rownum, string DisplayText, string fplink)
    {
      //Add text
      model.SetValue(rownum, 0, DisplayText);

      //Make hyperlink cell
      style = new StyleInfo();
      style.Reset();
      style.CellType = new HyperLinkCellType(fplink, null, "_self");
      style.VerticalAlign = VerticalAlign.Top;
      FpSpread1.ActiveSheetView.SetStyleInfo(rownum, 0, style);

    }

    public void InitSpread()
    {
      FpSpread1.Sheets[0].DataModel = model;

      FpSpread1.Sheets[0].GridLines = GridLines.None;
      FpSpread1.Sheets[0].PageSize = 10;
      FpSpread1.Sheets[0].OperationMode = OperationMode.ReadOnly;
      FpSpread1.Sheets[0].ColumnHeader.Visible = false;
      FpSpread1.Sheets[0].RowHeader.Visible = false;
      FpSpread1.CommandBar.Visible = false;

      FpSpread1.BorderStyle = BorderStyle.None;

      style = new StyleInfo();
      style.Reset();
      style.Font.Size = FontUnit.Point(10);
      style.Font.Name = "Verdana";
      FpSpread1.ActiveSheetView.SetStyleInfo(-1, -1, style);

      FpSpread1.Sheets[0].SetColumnWidth(0, 200);
      FpSpread1.Sheets[0].SetColumnWidth(1, 200);

    }

    public void SetStyleCartItems(int row)
    {
      style = new StyleInfo();
      style.Reset();
      //style.BackColor = Color.LightBlue
      //style.ForeColor = Color.White
      //style.Font.Bold = True
      style.Font.Italic = true;
      style.Font.Size = FontUnit.Point(8);
      style.HorizontalAlign = HorizontalAlign.Left;
      FpSpread1.ActiveSheetView.SetStyleInfo(row, 0, style);

    }


    private void Page_PreRender(object sender, System.EventArgs e)
    {
      //Turn on/off headers and grid lines
      if (ckShowSpread.Checked == true)
      {
        //Turn On headers
        FpSpread1.ActiveSheetView.ColumnHeader.Visible = true;
        FpSpread1.ActiveSheetView.RowHeader.Visible = true;
        FpSpread1.ActiveSheetView.GridLines = GridLines.Both;

      }
      else
      {
        //Turn Off headers
        FpSpread1.ActiveSheetView.ColumnHeader.Visible = false;
        FpSpread1.ActiveSheetView.RowHeader.Visible = false;
        FpSpread1.ActiveSheetView.GridLines = GridLines.None;

      }
    }
  }
}